package com.hdb.pipeline;

/*
 * 自定义txt文件输出类
 */
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.annotation.ThreadSafe;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import us.codecraft.webmagic.ResultItems;
import us.codecraft.webmagic.Task;
import us.codecraft.webmagic.pipeline.Pipeline;
import us.codecraft.webmagic.utils.FilePersistentBase;

@ThreadSafe
public class TestFilePipeline extends FilePersistentBase implements Pipeline {

	private Logger logger = LoggerFactory.getLogger(getClass());
	//记录储存记录的条数
	private int count;
	
	public int getCount() {
		return count;
	}

	public void setCount(int count) {
		this.count = count;
	}

	/**
	 * create a FilePipeline with default path"/data/webmagic/"
	 */
	public TestFilePipeline() {
		setPath("d:/data/webmagic/");
	}

	public TestFilePipeline(String path) {
		setPath(path);
	}

	public void process(ResultItems resultItems, Task task) {
		SimpleDateFormat sd = new SimpleDateFormat("yyyyMMdd");
		String data = sd.format(new Date());
		String path = this.path + PATH_SEPERATOR + task.getUUID() + PATH_SEPERATOR + data + PATH_SEPERATOR;
		try {

			PrintWriter printWriter = new PrintWriter(new OutputStreamWriter(
					new FileOutputStream(getFile(
							path + data + "_" + DigestUtils.md5Hex(resultItems.getRequest().getUrl()) + ".txt")),
					"UTF-8"));
			count++;
			for (Map.Entry<String, Object> entry : resultItems.getAll().entrySet()) {
				if (entry.getKey().equalsIgnoreCase("title")
						&& (entry.getValue() == null || StringUtils.isEmpty(entry.getValue().toString()))) {
					break;
				}
				if (entry.getValue() instanceof Iterable) {
					Iterable value = (Iterable) entry.getValue();
					printWriter.println(entry.getKey() + ":");
					for (Object o : value) {
						printWriter.println(o);
					}
				} else {

					if (entry.getValue() == null) {
						printWriter.println("[" + entry.getKey() + "]:");
					} else {
						String value = entry.getValue().toString();
						if (value.startsWith("[")) {
							value = value.substring(1);
						}
						if (value.endsWith("]")) {
							value = value.substring(0, value.length() - 1);
						}
						printWriter.println("[" + entry.getKey() + "]:" + value);
					}
				}
			}
			printWriter.close();
		} catch (IOException e) {
			logger.warn("write file error", e);
		}
	}
}
